/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package p2pscrabble;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.Point;

import javax.swing.JPanel;

/**
 * 
 * @author fred0002
 */
public class BoardPanel extends JPanel {
	private BoardTile boardTile[][];
	private char[][] tile;
	public static final int emptypanelheight = 29;
	private LayoutManager boardLayout;
	private char swapChar = '/';
	private boolean swapBlank = false;
	private char[][] boardLetter;
	private boolean[][] currentTurn;

	private TilesBag tiles;

	public BoardPanel() {
		boardTile = new BoardTile[15][15];
		tile = new char[15][15];
		boardLetter = new char[15][15];
		currentTurn = new boolean[15][15];
		tiles = new TilesBag();

		java.awt.Dimension size = new java.awt.Dimension(emptypanelheight * 15,
				emptypanelheight * 15);
		this.setMinimumSize(size);
		this.setPreferredSize(size);
		this.setMaximumSize(size);

		boardLayout = new FlowLayout(FlowLayout.CENTER, 0, 0);
		this.setLayout(boardLayout);
		this.setBackground(Color.white);

		initBoard();
	}

	private void initBoard() {
		String imageFile = "";
		String imageDir = ".\\img\\";

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				// initializing special tiles
				if (i == 7 && j == 7)
					tile[i][j] = 'a';
				else if (i % 7 == 0 && j % 7 == 0)
					tile[i][j] = 'b';
				else if (i > 0 && i < 5 && j > 0 && j < 5 && i == j)
					tile[i][j] = 'd';
				else if (i % 4 == 1 && j % 4 == 1)
					tile[i][j] = 'c';
				else if ((i == 0 && j == 3) || (i == 3 && j == 0)
						|| (i == 2 && j == 6) || (i == 6 && j == 2)
						|| (i == 3 && j == 7) || (i == 7 && j == 3)
						|| (i == 6 && j == 6))
					tile[i][j] = 'e';
				else
					tile[i][j] = 'f';

				if (i != 7 && j != 7) {
					tile[14 - i][14 - j] = tile[i][j];
				}
				if (i != 7 || j != 7) {
					tile[14 - i][j] = tile[i][j];
					tile[i][14 - j] = tile[i][j];
				}
			}
		}

		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				int lx = 1; // multiple letter score
				int wx = 1; // multiple word score

				boardLetter[i][j] = ' ';

				if (tile[i][j] == 'a') {
					imageFile = "swirl.jpg";
					wx = 2;
				} else if (tile[i][j] == 'b') {
					imageFile = "tripword.jpg";
					wx = 3;
				} else if (tile[i][j] == 'c') {
					imageFile = "triplet.jpg";
					lx = 3;
				} else if (tile[i][j] == 'd') {
					imageFile = "doubword.jpg";
					wx = 2;
				} else if (tile[i][j] == 'e') {
					imageFile = "doublet.jpg";
					lx = 2;
				} else {
					imageFile = "emptytile.jpg";
				}

				addTile(imageDir + imageFile, i, j, lx, wx);
			}
		}
	}

	private void addTile(String imageFile, int row, int col, int lx, int wx) {
		boardTile[row][col] = new BoardTile(imageFile, row, col, lx, wx);
		this.add(boardTile[row][col]);
	}

	public void setLetterTile(char letter, boolean isBlank, Point dest) {
		dest.x /= emptypanelheight;
		dest.y /= emptypanelheight;
		// System.out.println(dest.x + " " + dest.y);
		boardTile[dest.y][dest.x].setLetterTile(letter, isBlank);
	}

	public void removeTile(int row, int col) {
		boardTile[row][col].removeTile();
	}

	public void setTileFixed(int row, int col) {
		boardTile[row][col].setTileFixed();
	}
	
	public void removeRedLine() {
		for (int i=0; i<15; i++) {
			for (int j=0; j<15; j++) {
				boardTile[i][j].setLastTurn(false);
			}
		}
	}

	public char[][] getBoardLetter() {
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				boardLetter[i][j] = boardTile[i][j].getLetter();
			}
		}
		return boardLetter;
	}

	public boolean[][] getCurrentTurn() {
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				currentTurn[i][j] = boardTile[i][j].getThisTurn();
			}
		}
		return currentTurn;
	}

	public boolean checkBlankTileFilled() {
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				if (boardTile[i][j].getLetter() == ' '
						&& boardTile[i][j].getThisTurn())
					return false;
			}
		}
		return true;
	}

	public int computeScore(boolean horizontal, int row_or_col, int word_start,
			int word_end) {
		int multiply = 1;
		int total = 0;
		int i;

		for (i = word_start; i <= word_end; i++) {
			int lx = 1;
			if (horizontal) {
				if (boardTile[row_or_col][i].getThisTurn())
					lx *= boardTile[row_or_col][i].getLX();
				total += lx
						* tiles.getLetterScore(boardTile[row_or_col][i]
								.getLetter());
				if (boardTile[row_or_col][i].getThisTurn())
					multiply *= boardTile[row_or_col][i].getWX();
			} else {
				if (boardTile[i][row_or_col].getThisTurn())
					lx *= boardTile[i][row_or_col].getLX();
				total += lx
						* tiles.getLetterScore(boardTile[i][row_or_col]
								.getLetter());
				if (boardTile[i][row_or_col].getThisTurn())
					multiply *= boardTile[i][row_or_col].getWX();
			}
		}

		return (total * multiply);
	}

	public String getWord(boolean horizontal, int row_or_col, int word_start,
			int word_end) {
		String word = "";
		int i;
		for (i = word_start; i <= word_end; i++) {
			if (horizontal) {
				word += boardLetter[row_or_col][i];
			} else {
				word += boardLetter[i][row_or_col];
			}
		}
		return word;
	}

	public void submitOppTiles(char[] letters, int[] row, int[] col) {
		int i;
		boolean isBlank;
		for (i = 0; i < letters.length; i++) {
			if (letters[i] <= 90)
				isBlank = false;
			else
				isBlank = true;
			boardTile[row[i]][col[i]].setLetterTile(letters[i], isBlank);
		}
	}

	public void setBoardTileEnable(boolean b) {
		for (int i = 0; i < 15; i++)
			for (int j = 0; j < 15; j++)
				boardTile[i][j].setEnabled(b);
	}

	public char getSwapChar() {
		return swapChar;
	}

	public void setSwapChar(char letter) {
		swapChar = letter;
	}

	public boolean isSwapBlank() {
		return swapBlank;
	}

	public void setSwapBlank(boolean swapBlank) {
		this.swapBlank = swapBlank;
	}

	public void setBoardTiles(char[][] boardLetters) {
		boolean isBlank = false;
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				// reset the board letter 
				this.boardLetter[i][j] = boardLetters[i][j];
				
				if (boardLetters[i][j] == ' ') {
					boardTile[i][j].removeTile();
					boardTile[i][j].setLastTurn(false);
					continue;
				}
				
				if (boardLetters[i][j] <= 90)
					isBlank = false;
				else
					isBlank = true;
				boardTile[i][j].setLetterTile(boardLetters[i][j], isBlank);
				boardTile[i][j].setTileFixed();
				boardTile[i][j].setLastTurn(false);
			}
		}
	}
	
	public void returnToHand() {
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				if (boardTile[i][j].getThisTurn())
					boardTile[i][j].removeTile();
			}
		}
	}
}
